package cn.elwy.rcp.kit.builder.decode;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.elwy.common.util.io.FileUtil;

public class Decryptor {
	public static String ext = ".java";
	public static String extBack = ".java.back";

	public static void main(String[] args) throws IOException {
		String includeExtNames = ".*\\.java$|.*\\.cs$|.*\\.doc$|.*\\.docx$|.*\\.xls$|.*\\.xlsx$|.*\\.ppt$|.*\\.pptx$";

		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);

		System.out.println("1.备份文件    2.恢复文件");
		boolean inputState = true;

		File srcFile = null;
		File descFile = null;
		int exeType = in.nextInt();
		if (exeType == 1) {
			do {
				System.out.println("请输入原始目录或文件");
				String srcPath = in.next();
				if (srcPath == null || srcPath.equals("") || !new File(srcPath).exists()) {
					inputState = false;
				} else {
					inputState = true;
					srcFile = new File(srcPath);
				}
			} while (!inputState);
			do {
				System.out.println("请输入目标目录或文件");
				String descPath = in.next();
				if (descPath == null || descPath.equals("")) {
					inputState = false;
				} else {
					inputState = true;
					descFile = new File(descPath);
				}
			} while (!inputState);

			copyFiles(srcFile, descFile, "", includeExtNames);
			System.out.println("备份成功！");
		} else if (exeType == 2) {
			do {
				System.out.println("请输入原始目录或文件");
				String srcPath = in.next();
				if (srcPath == null || srcPath.equals("") || !new File(srcPath).exists()) {
					inputState = false;
				} else {
					inputState = true;
					srcFile = new File(srcPath);
				}
			} while (!inputState);
			renameTo(srcFile);
			System.out.println("重命名成功！");
		} else {
			return;
		}
	}

	public static boolean copyFiles(File srcFile, File destDir, String fileName, final String includeExtNames)
			throws IOException {
		if (!srcFile.exists()) { // 文件不存在
			return false;
		}
		if (!destDir.exists()) {
			destDir.mkdirs();
		}
		if (!destDir.isDirectory()) {
			return false;
		}
		boolean result = true;

		if (srcFile.isFile()) {
			fileName = srcFile.getName();
			File tempFile = new File(destDir, fileName + ext);
			if (includeExtNames != null && !includeExtNames.isEmpty()) {
				Pattern pattern = Pattern.compile(includeExtNames, Pattern.CASE_INSENSITIVE);
				Matcher matcher = pattern.matcher(fileName);
				if (matcher.find()) {
					result = FileUtil.copyFile(srcFile, destDir, fileName + ext, true);
					result = result & FileUtil.copyFile(tempFile, destDir, fileName + extBack, true);
					FileUtil.forceDelete(tempFile);
				}
			} else {
				result = FileUtil.copyFile(srcFile, destDir, fileName + ext, true);
				result = result & FileUtil.copyFile(tempFile, destDir, fileName + extBack, true);
				FileUtil.forceDelete(tempFile);
			}
		} else {
			destDir = new File(destDir, fileName);
			destDir.mkdirs();

			File[] children = srcFile.listFiles();
			if (children == null) {
				return true;
			}
			for (File file : children) {
				result = copyFiles(file, destDir, file.getName(), includeExtNames);
				if (!result) {
					return false;
				}
			}
		}
		return result;
	}

	public static boolean renameTo(File srcFile) throws IOException {
		if (!srcFile.exists()) { // 文件不存在
			return false;
		}
		boolean result = true;
		if (srcFile.isFile()) {
			String name = srcFile.getName();
			Pattern pattern = Pattern.compile(".*\\.java\\.back$", Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(name);
			if (matcher.find()) {
				String tempName = name.substring(0, name.length() - extBack.length());

				File destFile = new File(srcFile.getParentFile(), tempName);
				if (destFile.exists()) {
					destFile.delete();
				}
				return srcFile.renameTo(destFile);
			}
		} else {
			File[] children = srcFile.listFiles();
			if (children == null) {
				return true;
			}
			for (File file : children) {
				result = renameTo(file);
				if (!result) {
					return false;
				}
			}
		}
		return result;
	}

}
